﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportStore.Domain.Concrete;
using SportStore.Domain.Abstract;
using System.ComponentModel;
using SportStore.WebUI.Models;

namespace SportStore.WebUI.Controllers
{
    public class ProductsController : Controller
    {
        private IProductsRepository productsRepository;
        public int PageSize = 3;

        //public ProductsController()
        //{
        //    //productsRepository = new FakeProductsRepository();
        //    string connString = @"Server=.\sqlexpress;Database=SportStore;Trusted_Connection=yes;";
        //    productsRepository = new SqlProductsRepository(connString);
        //}

        public ProductsController(IProductsRepository productsRepository)
        {
            this.productsRepository = productsRepository;
        }

        //public ViewResult List()
        //{
        //    return View(productsRepository.Products.ToList());
        //}

        //
        // GET: /Products/

        public ActionResult Index()
        {
            return View();
        }


        public ViewResult List(string category, [DefaultValue(1)] int page)
        {
            var productsToShow = (category == null)
                                    ? productsRepository.Products
                                    : productsRepository.Products.Where(x => x.Category == category);

            var viewModel = new ProductsListViewModel
            {
                Products = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
                PagingInfo = new PageInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = productsToShow.Count()
                }, 
                CurrentCategory = category
            };
            
            return View(viewModel);
        }
    }
}
